﻿@model PackageOwnershipChangesModel
@{
    ViewBag.Title = "Package ownership - validate";
}

<section role="main" class="container main-container">
    <h1>@ViewBag.Title</h1>

    <h2>Input</h2>

    <ul class="list-unstyled">
        <li>
            <b>Package IDs:</b>
            <ul>
                @foreach (var packageId in Model.PackageIds)
                {
                    <li><a href="@Url.Package(packageId)">@packageId</a></li>
                }
            </ul>
        </li>
        <li><b>Requestor:</b> <a href="@Url.User(Model.Requestor)">@Model.Requestor.Username</a></li>
        <li>
            <b>Add owners:</b>
            @if (Model.AddOwners.Count == 0)
            {
                <text>(none)</text>
            }
            @for (var i = 0; i < Model.AddOwners.Count; i++)
            {
                <a href="@Url.User(Model.AddOwners[i])">@Model.AddOwners[i]</a>if (i < Model.AddOwners.Count - 1)
                {<text>,</text>}
        }
        </li>
        <li>
            <b>Remove owners:</b>
            @if (Model.RemoveOwners.Count == 0)
            {
                <text>(none)</text>
            }
            @for (var i = 0; i < Model.RemoveOwners.Count; i++)
            {
                <a href="@Url.User(Model.RemoveOwners[i])">@Model.RemoveOwners[i]</a>if (i < Model.RemoveOwners.Count - 1)
                {<text>,</text>}
        }
        </li>
        <li>
            <b>Message:</b>
            @if (string.IsNullOrEmpty(Model.Message))
            {
                <text>(none)</text>
            }
            else
            {
                <pre>@Model.Message</pre>
            }
        </li>
        <li>
            <b>Skip request flow:</b> @(Model.SkipRequestFlow ? "yes" : "no")
        </li>
    </ul>

    <h2>Change preview</h2>

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Package ID</th>
                <th title="Whether or not the requestor user has permissions to modify the package ownership">Permissions</th>
                <th title="Owners that will remain unchanged">Remaining</th>
                <th title="Ownership requests that will remain unchanged">Remaining requests</th>
                <th title="New owners that will be immediately added">Added</th>
                <th title="New ownership requests that will be sent">Added requests</th>
                <th title="Owners that will be immediately removed">Removed</th>
                <th title="Ownership requests that will be immediately removed">Removed requests</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var changes in Model.Changes)
            {
                <tr>
                    <td><a href="@Url.Package(changes.Id)">@changes.Id</a></td>
                    <td class="text-center">
                        @if (changes.RequestorHasPermissions)
                        {
                            <span title="Requestor @Model.Input.Requestor has permissions to change owners on @changes.Id">✔️</span>
                        }
                        else
                        {
                            <span title="Requestor @Model.Input.Requestor does not have permissions to change owners on @changes.Id">⚠️</span>
                        }
                    </td>
                    <td>
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.ExistingOwner))
                        {
                            <a title="@pair.Key is an existing owner of @changes.Id" href="@Url.User(pair.Key)">@pair.Key</a><br />
                        }
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.AlreadyOwner))
                        {
                            <b title="@pair.Key is already an owner of @changes.Id"><a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                    <td>
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.ExistingOwnerRequest))
                        {
                            <a title="@pair.Key has an existing ownership request for @changes.Id" href="@Url.User(pair.Key)">@pair.Key</a><br />
                        }
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.AlreadyOwnerRequest))
                        {
                            <b title="@pair.Key already has an ownership request for @changes.Id"><a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                    <td class="warning">
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.NewOwner))
                        {
                            <b title="@pair.Key will be immediately added as an owner of @changes.Id">➕ <a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                    <td class="warning">
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.NewOwnerRequest))
                        {
                            <b title="@pair.Key will have an ownership request sent for @changes.Id">➕ <a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                    <td class="warning">
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.RemoveOwner))
                        {
                            <b title="@pair.Key will be immediately removed as an owner from @changes.Id">➖ <a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                    <td class="warning">
                        @foreach (var pair in changes.UsernameToState.Where(x => x.Value.State == PackageOwnershipState.RemoveOwnerRequest))
                        {
                            <b title="@pair.Key will have an ownership request removed from @changes.Id">➖ <a href="@Url.User(pair.Key)">@pair.Key</a></b><br />
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <form method="get" action="@Url.Action("Index")">
        @Html.Partial("_HiddenForm", Model.Input)
        <button type="submit" class="btn btn-block btn-primary">Edit input</button>
    </form>
    <form method="post" action="@Url.Action("SubmitInput")">
        @Html.AntiForgeryToken()
        @Html.Partial("_HiddenForm", Model.Input)
        <button type="submit" class="btn btn-block btn-danger">Submit changes</button>
    </form>
</section>
